import json

from django.utils.six import BytesIO
from rest_framework.parsers import JSONParser

from api.tests.base_test_case import BaseTestCase
from app.models import Product, Category, ProductSpec, Cart


class CartTest(BaseTestCase):
    def setUp(self):
        super(CartTest, self).setUp()
        category = Category(name=u'豆叶类', description='XXXXX')
        category.save()
        product = Product(category=category, name=u'土豆', description=u'XXXXX')
        product.save()
        spec1 = ProductSpec(product=product, name=u'大土豆', unit=u'斤', price=1, stock=10, is_on_shell=True)
        spec1.save()

        spec2 = ProductSpec(product=product, name=u'小土豆', unit=u'斤', price=1, stock=10, is_on_shell=True)
        spec2.save()

        cart = Cart(user=self.user, spec=spec1, amount=1)
        cart.save()

    def test_get_cart(self):
        response = self.client.get('/api/cart/',
                                   content_type='application/json',
                                   accept_media_type='application/json',
                                   HTTP_AUTHORIZATION='Token ' + self.access_token)
        self.assertEqual(response.status_code, 200)
        data = JSONParser().parse(BytesIO(response.content))
        self.assertTrue(data)

    def test_add_cart(self):
        response = self.client.post('/api/cart/add/',
                                    data=json.dumps({'spec': 2, 'amount': 1}),
                                    content_type='application/json',
                                    accept_media_type='application/json',
                                    HTTP_AUTHORIZATION='Token ' + self.access_token)
        self.assertEqual(response.status_code, 200)
        data = JSONParser().parse(BytesIO(response.content))
        self.assertEquals(data['detail'], u'添加成功')

        response = self.client.post('/api/cart/add/',
                                    data=json.dumps({'spec': 1, 'amount': 1}),
                                    content_type='application/json',
                                    accept_media_type='application/json',
                                    HTTP_AUTHORIZATION='Token ' + self.access_token)
        self.assertEqual(response.status_code, 200)
        data = JSONParser().parse(BytesIO(response.content))
        self.assertEquals(data['detail'], u'添加成功')
        cart = Cart.objects.get(user=self.user, spec=1)
        self.assertEquals(cart.amount, 2)

        response = self.client.post('/api/cart/add/',
                                    data=json.dumps({'spec': 3, 'amount': 1}),
                                    content_type='application/json',
                                    accept_media_type='application/json',
                                    HTTP_AUTHORIZATION='Token ' + self.access_token)
        self.assertEqual(response.status_code, 400)

    def test_delete_cart(self):
        carts = Cart.objects.all()
        response = self.client.delete('/api/cart/delete/',
                                      accept_media_type='application/json',
                                      content_type='application/json',
                                      data=json.dumps({"carts": [{"cart_id": 1}, {"cart_id": 2}]}),
                                      HTTP_AUTHORIZATION='Token ' + self.access_token)
        self.assertEqual(response.status_code, 200)
        data = JSONParser().parse(BytesIO(response.content))
        self.assertEquals(data['detail'], u'删除成功')

    def test_update_cart(self):
        response = self.client.put('/api/cart/update/',
                                   data=json.dumps({'spec': 1, 'amount': 3}),
                                   content_type='application/json',
                                   accept_media_type='application/json',
                                   HTTP_AUTHORIZATION='Token ' + self.access_token)
        self.assertEqual(response.status_code, 200)
        data = JSONParser().parse(BytesIO(response.content))
        self.assertEquals(data['detail'], u'更新成功')

        cart = Cart.objects.get(spec=1)
        self.assertEqual(cart.amount, 3)
